import logging
import logging.config
import os
import traceback

from src.utils import context_request_id

if not os.path.isdir('logs'):
    os.mkdir('logs')

path = os.path.abspath(os.path.join(os.getcwd(), "."))
logdir = path + '/etc/log.conf'
logging.config.fileConfig(path + '/etc/log.conf')

logging.getLogger("werkzeug").setLevel(logging.INFO)
logging.getLogger("sqlalchemy").setLevel(logging.WARNING)


class MyFilter(logging.Filter):
    def filter(self, record):
        request_id = ""
        try:
            request_id = context_request_id.get("-")
        except Exception:
            print(traceback.format_exc())
        record.request_id = request_id
        return True


my_filter = MyFilter()
for handler in logging.getLogger().handlers:
    handler.addFilter(my_filter)
